/*
 * Copyright (C) 2023, KylinSoft Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
**/

#ifndef BLUETOOTHMAINWINDOW_H
#define BLUETOOTHMAINWINDOW_H

#include <QIcon>
#include <QTimer>
#include <QLabel>
#include <QFrame>
#include <QDebug>
#include <QObject>
#include <QString>
#include <QWidget>
#include <QComboBox>
#include <QMetaEnum>
#include <QMainWindow>
#include <QStringList>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QStackedWidget>

#include "loadinglabel.h"
#include "kswitchbutton.h"
#include "bluetoothnamelabel.h"

#include "bluetoothdeviceitem.h"

#define MAINWINDOW_ERROR_ABNORMAL_DRIVE             0
#define MAINWINDOW_ERROR_ADAPTER_NOT_DETECTED       1
#define MAINWINDOW_LOADING_INTERFACE                2
#define MAINWINDOW_NORMAL_INTERFACE                 3

using namespace kdk;

class BlueToothMainWindow : public QMainWindow
{
    Q_OBJECT
public:
    enum _DEV_TYPE {
        BT_All = 0,
        BT_Audio,
        BT_Peripherals,
        BT_Computer,
        BT_Phone,
        BT_Other,
    };
    Q_ENUM(_DEV_TYPE);

    //记录当前是否蓝牙界面是否在操作状态
    static bool m_device_operating; //有设备正在操作

    //
    const QStringList devTypeSelectStrList = {tr("All"), \
                                              tr("Audio"), \
                                              tr("Peripherals"), \
                                              tr("Computer"), \
                                              tr("Phone"), \
                                              tr("Other")};

    explicit BlueToothMainWindow(QWidget *parent = nullptr);
    ~BlueToothMainWindow();

    void InitInterfaceUIStatus();
protected:
    void InitMainWindow();
    void InitAbnormalErrorWidget();
    void InitNoAdapterErrorWidget();
    void InitAdapterLoadingWidget();
    void setAdapterLoadingWidget(int m_mode);
    void InitNormalWidget();

    void InitNormalWidgetTop();
    void InitNormalWidgetMiddle();
    void InitNormalWidgetBottom();

    void InitBluetoothServiceSignalBinding();
    void InitInterfaceUISignalBinding();

public slots:

    void addMyDeviceItemUI(QString);
    void addOneBluetoothDeviceItemUi(QString);

private slots:
    //界面槽
    void setDefaultAdapterNameSlot(QString);

    void _BtSwitchBtnSlot(bool status);
    void adapterPowerStatusChangedSlot(bool);
    void adapterNameChangedSlot(QString);

    void _BtTrayIconShowSlot(bool status);
    void adapterTrayIconSlot(bool status);
    void _BtDiscoverableSlot(bool status);
    void adapterDiscoverableSlot(bool status);
    void _BtAutoAudioConnBtnSlot(bool status);
    void adapterActiveConnectionSlot(bool status);
    void adapterDiscoveringSlot(bool status);

    void _AdapterListSelectComboBoxSlot(int indx);
    void _DevTypeSelectComboBoxSlot(int indx);

    //dev slot
    void adapterAddSlot(QString adapter_name);
    void adapterRemoveSlot(int indx);
    void defaultAdapterChangedSlot(int);

    void deviceAddSlot(QString);
    void deviceRemoveSlot(QString);
    void devicePairedSuccessSlot(QString);

    void btServiceRestartSlot();
    void btServiceRestartCompleteSlot(bool);

private:

    QStackedWidget *_MCentralWidget = nullptr;
    QWidget        *_MAbnormalErrorWidget  = nullptr;
    QWidget        *_MNoAdapterErrorWidget  = nullptr;
    QWidget        *_MAdapterLoadingWidget  = nullptr;
    QWidget        *_MNormalWidget  = nullptr;

    QVBoxLayout *_NormalWidgetMainLayout = nullptr;
    QVBoxLayout *_NormalWidgetPairedDevLayout = nullptr;
    QVBoxLayout *_NormalWidgetCacheDevLayout = nullptr;

    QWidget *_MNormalWidgetTop = nullptr;
    QWidget *_MNormalWidgetMiddle = nullptr;
    QWidget *_MNormalWidgetBottom = nullptr;

    QFrame  *_MNormalFrameTop = nullptr;
    QFrame  *_MNormalFrameMiddle = nullptr;
    QFrame  *_MNormalFrameBottom = nullptr;

    QFrame  *BtSwitchLineFrame = nullptr;
    QFrame  *BtAdapterListFrame = nullptr;
    QFrame  *BtAdapterListLineFrame = nullptr;
    QFrame  *BtTrayIconShowFrame= nullptr;
    QFrame  *BtTrayIconShowLineFrame = nullptr;
    QFrame  *BtDiscoverableFrame= nullptr;
    QFrame  *BtDiscoverableLineFrame= nullptr;
    QFrame  *BtAutomaticAudioConnectionFrame= nullptr;

    KSwitchButton   *_BtSwitchBtn = nullptr;
    KSwitchButton   *_BtTrayIconShow = nullptr;
    KSwitchButton   *_BtDiscoverable = nullptr;
    KSwitchButton   *_BtAutoAudioConnBtn = nullptr;

    LoadingLabel        *_LoadIcon = nullptr;
    BluetoothNameLabel  *_BtNameLabel = nullptr;
    QComboBox           *_AdapterListSelectComboBox = nullptr;
    QComboBox           *_DevTypeSelectComboBox = nullptr;

    QMap <QString, QString> adapterDataMap;
    QList <QString> adapterAddressList;
    QList <QString> adapterNameList;

    int  m_loadingWindx = 0;
    QTimer * m_loadingWBTAdapterTimer = nullptr;
    QTimer * m_loadingWBTServiceTimer = nullptr;
//    QTimer * m_loadingWIcontimer = nullptr;

    BlueToothDBusService * btServer = nullptr;
    _DEV_TYPE currentShowTypeFlag = _DEV_TYPE::BT_All;
    bool _MyDevicesInterfaceShowFlags = false;
    bool _BTServiceReportPowerSwitchFlag = false;           //服务上报的蓝牙开关状态
    bool _BTServiceReportTrayIconSwitchFlag = false;        //服务上报的蓝牙托盘显示开关状态
    bool _BTServiceReportDiscoverableSwtichFlag = false;    //服务上报的可被发现状态
    bool _BTServiceReportAutoAudioConnSwtichFlag = false;    //服务上报的自动音频状态
//    bool _BTServiceReportDiscoveringSwtichFlag = false;     //服务上报的扫描状态

    bool isEffectiveDevice(QString, bluetoothdevice::DEVICE_TYPE);
    bool whetherToDisplayInTheCurrentInterface(bluetoothdevice::DEVICE_TYPE devType);

    void reloadDeviceListItem(_DEV_TYPE flag);
    void frameAddLineFrame(int,QString,QString);
    void removeBluetoothDeviceItemUi(QString);
    void removeMDevFrameLineFrame(QString);
    void adjustDeviceDisplayPosition(QString ,quint16);

    int  getDevRssiItemInsertIndex(qint16 currDevRssi);
    void changeDeviceParentWindow(const QString &address);
    void adapterChangedRefreshInterface(int indx);

    void clearMyDevicesUI();
    void clearOtherDevicesUI();
};

#endif // BLUETOOTHMAINWINDOW_H
